package demo

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/demo"
	demoReq "github.com/flipped-aurora/gin-vue-admin/server/model/demo/request"
)

type SysTeacherService struct {
}

func (SysTeacherService *SysTeacherService) CreateTeacher(sysTeacher *demo.SystemTeacher) (err error) {
	err = global.GVA_DB.Create(sysTeacher).Error
	return err
}

func (SysTeacherService *SysTeacherService) DeleteSysTeacher(sysTeacher *demo.SystemTeacher) (err error) {
	err = global.GVA_DB.Delete(sysTeacher).Error
	return err
}

func (SysTeacherService *SysTeacherService) DeleteSysTeacherByIds(ids *request.IdsReq) (err error) {
	//var sysTeacher demo.SystemTeacher
	//err = global.GVA_DB.Table(sysTeacher.TableName()).Delete("id in ? ", ids).Error
	err = global.GVA_DB.Delete(&[]demo.SystemTeacher{}, "id in ?", ids.Ids).Error
	//err = global.GVA_DB.Delete(&[]demo.SysStudent{}, "id in ?", ids.Ids).Error
	return err

}

// 保存or更新
func (SysTeacherService *SysTeacherService) UpdateSysTeacher(sysTeacher *demo.SystemTeacher) (err error) {
	err = global.GVA_DB.Save(sysTeacher).Error
	return err
}

func (SysTeacherService *SysTeacherService) GetSysTeacherById(id uint) (sysTeacher demo.SystemTeacher, err error) {
	err = global.GVA_DB.Where("id =  ?", id).Find(&sysTeacher).Error
	return
}

func (SysTeacherService *SysTeacherService) ListSysTeacher(param demoReq.SysTeacherParam) (list []demo.SystemTeacher, total int64, err error) {
	pageSize := param.PageSize
	offset := pageSize * (param.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&demo.SystemTeacher{})
	if param.Name != "" {
		db.Where("name = ?", param.Name)
	}
	//统计数量
	db.Limit(pageSize).Offset(offset).Count(&total)
	//查询结果
	err = db.Limit(pageSize).Offset(offset).Find(&list).Error
	return list, total, err
}
